package godardbaudrin.rsa.decrypt;

/**
 * 
 * @author Destiny
 *
 */
public class DecryptFormulas{

	private final double e;
	private final double n;
	
	protected DecryptFormulas(double e, double n){
		this.e = e;
		this.n = n;
	}
	
	//TODO write decrypt methods
	
	/**
	 *@return an array with all premium numbers < root N
	 */
	protected int[] getPN() {
		int rootN = (int) (Math.sqrt(n));
		
		int[] pn = new int[rootN];
        pn[0] = 2; // First number is 2
        int lg = 1; // First number found
        int i = 3; // First number which will be tested
       
        for(int j = 0; j < lg; j++)
        {
                if(( i % pn[j]) == 0){//If tested number is a premium number's multiple
                        j = 0;//Counter reinitialized
                        i = i + 2;//Exclusion of multiples of 2
                }
                else{
                        while( pn[j] > Math.sqrt(i) ){
                                pn[lg] = i;//Storing premium number in array
                                lg++;//Increase premium number
                                j = 0;//Counter reinitialized
                                i = i + 2;//For a new number
                        }                    
                }
                
                if(lg == pn.length)// Out of "for" if array reach limit
                	break;
        }
        
        int[] pnList = new int[lg];
        
        for (i = 0; i < pnList.length; i++)
        	pnList[i] = pn[i];//Copy array in new one for fix array limit
		    
		    return pnList;
	} 
	
	public int getP(){
		int[] pnList = getPN();
		double storeValue;
		int storeValue2;
		
		for (int i = 0; i < pnList.length; i++){
			storeValue = n/pnList[i];
			storeValue2 = (int) (n/pnList[i]);
			
			if (storeValue == storeValue2)
				return pnList[i];
			//System.out.println(storeValue);
		}
		
		return 0;
	}
	
	public int getQ(){
		return (int) (n/getP());
	}
	
	public double getE(){
		return e;
	}
	
	public double getN(){
		return n;
	}
}
